//
//  NotificationTools.m
//  Birthday Reminder
//
//  Created by Andrei Marcu on 11/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NotificationTools.h"
#import "DateTools.h"


@implementation NotificationTools

+ (void)cancelAllNotificationExceptCustomOnes {
    NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    for (UILocalNotification *notification in notifications) {
        if (notification.alertBody == nil) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }
    }
}

+ (void)updateNotificationsFromDictionary:(NSDictionary *)birthdayItems {
    [self cancelAllNotificationExceptCustomOnes];
    NSMutableDictionary *notificationDictionary = [[NSMutableDictionary alloc] init];
    for (NSDate *dateKey in [birthdayItems allKeys]) {
        for (BirthdayItem *item in [birthdayItems objectForKey:dateKey]) {
            NSString *badgeNumberString = [notificationDictionary objectForKey:item.nextBirthday];
            if (!badgeNumberString) {
                [notificationDictionary setObject:@"1" forKey:item.nextBirthday];
            } else {
                NSInteger badgeNumber = [badgeNumberString intValue];
                ++badgeNumber;
                [notificationDictionary setObject:[NSString stringWithFormat:@"%d", badgeNumber] forKey:item.nextBirthday];
            }
        }
    }
    for (NSDate *currentKey in [notificationDictionary allKeys]) {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        UILocalNotification *nextDayLocalNotification = [[UILocalNotification alloc] init];
        if (localNotif == nil) {
            [localNotif release];
            [nextDayLocalNotification release];
            [notificationDictionary release];
            return;
        }
        localNotif.fireDate = currentKey;
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        
        NSDateComponents *dateComp = [DateTools getDateComponentsFromDate:currentKey];
        NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        [cal setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
        [dateComp setDay:[DateTools getDayFromDate:currentKey] + 1];
        [dateComp setHour:0];
        [dateComp setMinute:1];
        NSDate *nextDay = [cal dateFromComponents:dateComp];
        nextDayLocalNotification.fireDate = nextDay;
        nextDayLocalNotification.timeZone = [NSTimeZone defaultTimeZone];
        
        NSString *currentBageNumber = [notificationDictionary objectForKey:currentKey];
        localNotif.applicationIconBadgeNumber = [currentBageNumber intValue];
        
        nextDayLocalNotification.applicationIconBadgeNumber = 0;
        
        // Schedule the notification
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        [localNotif release];
        [[UIApplication sharedApplication] scheduleLocalNotification:nextDayLocalNotification];
        [nextDayLocalNotification release];
        [cal release];
    }
    [notificationDictionary release];
}

@end
